package com.shengyuan.manage_os.old.ying.erpm;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.shengyuan.manage_os.R;
import com.shengyuan.manage_os.old.ying.erpm.data.MenuItem;
import com.shengyuan.manage_os.old.ying.erpm.db.Actual_Data_Table;
import com.shengyuan.manage_os.old.ying.erpm.layout.GridAdapter;
import com.shengyuan.manage_os.old.ying.erpm.layout.PageIndicator;
import com.shengyuan.manage_os.old.ying.erpm.layout.Workspace;
import com.shengyuan.manage_os.old.ying.erpm.layout.Workspace.OnViewChangedListener;
import com.shengyuan.manage_os.old.ying.erpm.util.Utility;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.widget.GridView;

public abstract class WorkSpaceActivity extends Activity implements OnViewChangedListener {
	public static WorkSpaceActivity Instance;
    protected Workspace mWorkspace;
    protected PageIndicator mIndicator;
    public static HashMap<String, String> ProjectName , ProjectName_BaoXiao;
    public static Date ShiShiTimeE;
    
    public static String[] A_PROJECTNAME, A_TASKNAME;
    public abstract Handler GetHandler();
    public abstract List<MenuItem>  LoadMenuListItems();
    public abstract boolean IsAllow();
    public int mVerticalSpacing = 30;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (IsAllow()) {
            Instance = this;
            setContentView(R.layout.ziyoujixie);
            mWorkspace = (Workspace) findViewById(R.id.workspace);
            mIndicator = (PageIndicator) findViewById(R.id.indicator);
            LoadMenuItems();
		}else {
			Utility.MessageBox(this,"你没有被授权访问这个部门!");
			finish();
			return;
		}
        GetProjectDataList();
    }
    
    public void GetProjectDataList() {

	}
    
    @Override
	protected void onResume() {
    	
		super.onResume();
	}

    protected void LoadMenuItems() {
    	int k =15;
	    List<MenuItem> MenuItems = LoadMenuListItems(); 
	    if (MenuItems==null) {
			Utility.MessageBox(this,"当前你没有被分配项目实施工作!");
			finish();
			return;
		}
//        if (MenuItems.size()==0) {
//        	Utility.MessageBox("没有可打印的凭条!");
//        	this.finish();
//		}else {
			//mWorkspace.removeAllViewsInLayout();
	        for (int i = 0; i < Math.ceil(1.0f * MenuItems.size() / Workspace.APP_PAGE_SIZE); i++) {
	            GridView grid = new GridView(this);
	            grid.setNumColumns(1);
	            grid.setHorizontalSpacing(5);
	            grid.setVerticalSpacing(k);
	            grid.setPadding(30, 50, 30, 20);
	            grid.setGravity(Gravity.CENTER);
	            grid.setAdapter(new GridAdapter(this, MenuItems, i));
	            mWorkspace.addView(grid);
	        }
	        mWorkspace.setOnViewChangedListener(this);
	        mIndicator.setIndication(mWorkspace.getChildCount(), 0);
//		}	
	}
    
    @Override
    public void onChange(int cnt, int index) {
        mIndicator.setIndication(cnt, index);
    }
    
    public  Handler mHandler= new Handler(){
		@Override
		public void handleMessage(Message msg) {
			ArrayList<String> Json1, Json2;
			ArrayList<Integer>  mRowid;
			ArrayList<Integer>  mPrindCount;
			ArrayList<String>  mOverTime;
			if (msg.what==1) {
				Actual_Data_Table dbo = new Actual_Data_Table(WorkSpaceActivity.this);
				Json1 = dbo.getArray_AllFinished_ForPrinter(1);
//				Json1 = dbo.getArray_LastFinished_ForPrinter(1);
				Json2 = dbo.Json2;
				mRowid = dbo.RowID;
				mPrindCount = dbo.PrintCount;
				mOverTime = dbo.OverTime;
				dbo.close(); 
	    	    Intent intent = new Intent();
	    	    Actual_Form_PrintFinished.RowId = mRowid.get(0);
	    	    Actual_Form_PrintFinished.Json1 = Json1.get(0);
	    	    Actual_Form_PrintFinished.Json2 = Json2.get(0);
	    	    Actual_Form_PrintFinished.mPrindCount = mPrindCount.get(0);
	    	    Actual_Form_PrintFinished.overtime = mOverTime.get(0);


	    	    intent.setClass(WorkSpaceActivity.this, Actual_Form_PrintFinished.class);
	    	    startActivity(intent);
	    	    
//				Intent intent = new Intent(WorkSpaceActivity.Instance,
//						FinishedListActivity.class );
//				startActivity(intent);
			}
		}
    	
    };
}

